package com.xs.shop.orders.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xs.shop.common.exceptions.BusinessException;
import com.xs.shop.common.exceptions.enums.Resp;
import com.xs.shop.orders.model.dto.CartDTO;
import com.xs.shop.orders.model.entity.Cart;
import com.xs.shop.orders.service.handlers.BaseContextHandler;
import com.xs.shop.orders.service.mapper.CartMapper;
import com.xs.shop.orders.service.service.CartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 购物车服务实现类
 * </p>
 *
 * @author xs,zyx
 * @since 2025-07-16
 */
@Slf4j
@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements CartService {

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addCart(CartDTO cartDTO) {
        Cart cart = new Cart();
        BeanUtils.copyProperties(cartDTO, cart);
        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Cart::getMemberId, cartDTO.getMemberId());
        wrapper.eq(Cart::getGoodsId, cartDTO.getGoodsId());
        //检查商品是否已经存在
        Cart item = this.getOne(wrapper);
        if(item!=null){
            item.setQuantity(item.getQuantity()+cartDTO.getQuantity());
            this.updateById(item);
            return ;
        }
        this.save(cart);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteCart() {
        Long memberId = BaseContextHandler.get();
        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Cart::getMemberId, memberId);
        // 删除购物车项
        return this.remove(wrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Cart updateCart(CartDTO cartDTO) {
        // 构建查询条件：memberId + goodsId
        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Cart::getMemberId, cartDTO.getMemberId());
        wrapper.eq(Cart::getGoodsId, cartDTO.getGoodsId());

        // 查询购物车记录
        Cart cart = this.getOne(wrapper);
        if (cart == null) {
            log.info("购物车中没有该商品或者用户");
        }// 更新数量
        else cart.setQuantity(cartDTO.getQuantity());

        // 执行更新
        this.update(cart, wrapper);

        log.info("购物车更新成功：{}", cart);
        return cart;
    }

    @Override
    public List<Cart> getCart() {
        Long memberId = BaseContextHandler.get();
        // 构建查询条件
        LambdaQueryWrapper<Cart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Cart::getMemberId, memberId);
        // 查询会员购物车列表
        return this.list(queryWrapper);
    }

    @Override
    public void ruduceCart(CartDTO cartDTO) {
        Cart cart = new Cart();
        BeanUtils.copyProperties(cartDTO, cart);
        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Cart::getMemberId, cartDTO.getMemberId());
        wrapper.eq(Cart::getGoodsId, cartDTO.getGoodsId());
        //检查商品是否已经存在
        Cart item = this.getOne(wrapper);
        if(item == null){
            throw new BusinessException(Resp.INVALID_PARAM);
        }
        if (item.getQuantity() <= 1) {
            // 删除商品记录
            this.remove(wrapper);
        }else{
            item.setQuantity(item.getQuantity() - cartDTO.getQuantity());
            this.updateById(item);
        }

    }
}
